//
//  XMSearchNavView.m
//  XunMi
//
//  Created by MacBook on 2024/4/28.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMSearchNavView.h"

@interface XMSearchNavView () <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIButton *typeBtn;
@property (nonatomic, copy) void(^changeBlock)(void);
@property (nonatomic, copy) void(^clearBlock)(void);
@property (nonatomic, copy) void(^searchBlock)(XMSearchType type, NSString *text);
@end

@implementation XMSearchNavView

- (void)awakeFromNib {
    [super awakeFromNib];
    self.textField.autocorrectionType = UITextAutocorrectionTypeNo;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [_typeBtn setImagePosition:XMImagePositionRight spacing:10];
}

- (void)setType:(XMSearchType)type {
    _type = type;
    switch (type) {
        case XMSearchByName:
        {
            [self.typeBtn setTitle:@"昵称" forState:UIControlStateNormal];
        }
            break;
        case XMSearchByCity:
        {
            [self.typeBtn setTitle:@"城市" forState:UIControlStateNormal];
        }
            break;
            
        default:
            break;
    }
    
    self.textField.placeholder = kStringFormat(@"请输入您要搜索的%@", self.typeBtn.titleLabel.text);
    
}

- (void)setupChangeBlock:(void (^)(void))changeBlock 
             searchBlock:(void (^)(XMSearchType, NSString * _Nonnull))searchBlock
              clearBlock:(void (^)(void))clearBlock {
    _changeBlock = changeBlock;
    _searchBlock = searchBlock;
    _clearBlock = clearBlock;
}

- (IBAction)typeAct:(id)sender {
    if (self.changeBlock) {
        self.changeBlock();
    }
}

- (IBAction)searchAct:(id)sender {
    if (self.searchBlock) {
        self.searchBlock(self.type, self.textField.text);
    }
}

- (IBAction)textFieldDidChange:(UITextField *)sender {
    if ([XMUtil isBlankString:sender.text]) {
        if (self.clearBlock) {
            self.clearBlock();
        }
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    return [textField resignFirstResponder];
}

- (void)dealloc {
    NSLog(@"释放了%@",[self class]);
    //    NSLog(@"释放了 -[%@ %s]",NSStringFromClass([self class]), sel_getName(_cmd));
}

@end
